/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.bootstrap.template.client;

import java.util.List;

import com.cleangwt.bootstrap.client.ui.Affix;
import com.cleangwt.bootstrap.client.ui.Navbar;
import com.cleangwt.bootstrap.client.ui.Affix.AffixOffset;
import com.cleangwt.page.client.PageContainer;
import com.cleangwt.page.client.ResizePage;
import com.cleangwt.user.shared.Link;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * A {@link PageContainer} with {@link Navbar} and affixed {@link Sidebar}.
 * 
 * <p>
 * #For displaying correctlly with affix, you had better not to present {@link ResizePage} on this
 * container.
 * <p>
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class NavbarAffixedContainer extends Composite implements PageContainer {
  private class SidebarToggleHandler implements CloseHandler<Sidebar>, OpenHandler<Sidebar> {

    @Override
    public void onClose(CloseEvent<Sidebar> event) {
      closeSidebar();
    }

    @Override
    public void onOpen(OpenEvent<Sidebar> event) {
      openSidebar();
    }

  }

  private static final String HEADING_STYLE = "affixed-heading";
  private static final String AFFIX_CONTAINER_STYLE = "affix-container";
  private static final String SIDEBAR_CONTAINER = "sidebar-container";
  private static final String VIEWPORT_STYLE = "affixed-viewport";
  private static final String FOOTER_STYLE = "affixed-footer";

  private static final int SIDEBAR_MARGIN_TOP = 10;

  protected DockPanel layout = new DockPanel();
  protected Navbar navbar = new Navbar();
  private SimplePanel viewport = new SimplePanel();

  private SimplePanel heading = new SimplePanel();
  private SimplePanel footer = new SimplePanel();
  private SimplePanel affixContainer = new SimplePanel();

  protected SimplePanel sidebarContainer = new SimplePanel();
  protected SidebarToggleHandler toggleHandler = new SidebarToggleHandler();
  protected List<Link> links;

  /**
   * Creates a navbar-affixed page container with the given links.
   * 
   * @param navbarStyle the navbar's type style
   * @param links the links
   */
  public NavbarAffixedContainer(String navbarStyle, List<Link> links) {
    this.links = links;
    initWidget(layout);
    setWidth("100%");

    layout.add(navbar, DockPanel.NORTH);
    layout.add(heading, DockPanel.NORTH);
    layout.add(affixContainer, DockPanel.WEST);
    layout.add(footer, DockPanel.SOUTH);
    layout.add(viewport, DockPanel.CENTER);

    navbar.setStyleBase(navbarStyle);
    navbar.addStyleName(Navbar.FIXED_TOP_STYLE);

    heading.setStyleName(HEADING_STYLE);

    affixContainer.setStyleName(AFFIX_CONTAINER_STYLE);
    affixContainer.add(sidebarContainer);
    sidebarContainer.setStyleName(SIDEBAR_CONTAINER);
    sidebarContainer.getElement().getStyle().setMarginTop(SIDEBAR_MARGIN_TOP, Unit.PX);

    footer.setStyleName(FOOTER_STYLE);

    viewport.setStyleName(VIEWPORT_STYLE);

    Affix.activate(sidebarContainer.getElement(), new AffixOffset() {

      @Override
      public int bottom() {
        return getFooterHeight();
      }

      @Override
      public int top() {
        return getHeadingHeight();
      }
    });
  }

  @Override
  public void clearPage() {
    viewport.setWidget(null);
  }

  /**
   * Closes the sidebar.
   */
  protected void closeSidebar() {
    sidebarContainer.setWidth(Sidebar.TOGGLE_WIDTH + "px");
    affixContainer.setWidth(Sidebar.TOGGLE_WIDTH + "px");
    layout.setCellWidth(affixContainer, Sidebar.TOGGLE_WIDTH + "px");
  }

  /**
   * Gets the footer height.
   * 
   * @return the footer height
   */
  protected int getFooterHeight() {
    return footer.getElement().getParentElement().getOffsetHeight();
  }

  /**
   * Gets the heading height.
   * 
   * @return the heading height
   */
  protected int getHeadingHeight() {
    return heading.getElement().getParentElement().getOffsetHeight() - navbar.getOffsetHeight();
  }

  /**
   * Gets the navbar.
   * 
   * @return the navbar
   */
  public Navbar getNavbar() {
    return navbar;
  }

  /**
   * Opens the sidebar.
   */
  protected void openSidebar() {
    sidebarContainer.setWidth(Sidebar.VIEW_WIDTH + "px");
    affixContainer.setWidth(Sidebar.VIEW_WIDTH + "px");
    layout.setCellWidth(affixContainer, Sidebar.VIEW_WIDTH + "px");
  }

  @Override
  public void setContentPage(String pageName, Widget page) {
    viewport.setWidget(page);
  }

  /**
   * Sets the footer widget.
   * 
   * @param w the widget to set
   */
  public void setFooter(Widget w) {
    footer.setWidget(w);
  }

  /**
   * Sets the heading widget.
   * 
   * @param w the widget to set
   */
  public void setHeading(Widget w) {
    heading.setWidget(w);
  }

}
